﻿using System;
using System.Collections.Generic;
using System.Text;
using GoShopping.Model;

namespace GoShopping.BLL.PrdctInfo
{
    /// <summary>
    /// 进货操作类
    /// </summary>
    public class StockProductBean
    {
        /// <summary>
        /// 添加一次进货
        /// [进货的同时更新商品中的库存信息]
        /// </summary>
        /// <param name="sp"></param>
        /// <returns>是否成功</returns>
        public bool AddOnceStock(StockProduct sp)
        {
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required))
            {
                int i = Databases.DBGoShopping.Insert(GoShoppingNBear.StockProduct).AddColumn(
                    GoShoppingNBear.StockProduct.Durability, sp.Durability).AddColumn(
                    GoShoppingNBear.StockProduct.InCount, sp.InCount).AddColumn(
                    GoShoppingNBear.StockProduct.InDate, sp.InDate).AddColumn(
                    GoShoppingNBear.StockProduct.InPrice, sp.InPrice).AddColumn(
                    GoShoppingNBear.StockProduct.MarketPrice, sp.MarketPrice).AddColumn(
                    GoShoppingNBear.StockProduct.ProduceDate, sp.ProduceDate).AddColumn(
                    GoShoppingNBear.StockProduct.ProductID, sp.ProductID).AddColumn(
                    GoShoppingNBear.StockProduct.SalePrice, sp.SalePrice).Execute();
                if (i < 0)
                {
                    return false;
                }
                string sql = string.Empty;
                sql = "UPDATE " + GoShoppingNBear.Product.___GetTableName() + " SET " +
                    GoShoppingNBear.Product.Count.Name + "=" +
                    GoShoppingNBear.Product.Count.Name + "+" + sp.InCount.ToString() +
                    " WHERE " + GoShoppingNBear.Product.ID.Name + "='" + sp.ProductID.ToString() + "'";
                i = Databases.DBGoShopping.CustomSql(sql).ExecuteNonQuery();
                if (i < 0)
                {
                    return false;
                }
                scope.Complete();
                return true;
            }
        }

        public IList<StockProduct> GetAllList()
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.StockProduct).OrderBy(
                GoShoppingNBear.StockProduct.ID.Desc
                ).ToList<StockProduct>();
        }

        /// <summary>
        /// 查找最近一个进货
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public Decimal? FindLastestInPrice(int productid)
        {
            List<StockProduct> list= Databases.DBGoShopping.Select(
                GoShoppingNBear.StockProduct, GoShoppingNBear.StockProduct.InPrice).Where(
                GoShoppingNBear.StockProduct.ProductID== productid).OrderBy(
                GoShoppingNBear.StockProduct.InDate.Desc).ToList < StockProduct>();
            if (list.Count > 0)
            {
                return list[0].InPrice;
            }
            else
            {
                return null;
            }
        }

    }
}
